package saleMarket.service;


import saleMarket.dao.ShopOwnerDao;

import saleMarket.domain.ShopOwner;
import saleMarket.domain.User;
import saleMarket.domain.authority.Actor;
import saleMarket.service.authority.ActorService;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class ShopOwnerService {
    private ShopOwnerDao shopOwnerDao = ShopOwnerDao.getInstance();
    private static ShopOwnerService shopOwnerService = new ShopOwnerService();

    public static ShopOwnerService getInstance(){return shopOwnerService;}

    public ShopOwner find(Integer id)throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        ShopOwner shopOwner = null;
        try {
            shopOwner = shopOwnerDao.find(id,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return shopOwner;
    }

    public Collection<ShopOwner> findAll()throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        Collection<ShopOwner> shopOwners = null;
        try {
            shopOwners = shopOwnerDao.findAll(conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return shopOwners;
    }

    public boolean add(ShopOwner shopOwner)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean added = false;
        try {
            Actor actor = ActorService.getInstance().find(2);
            System.out.println(actor);
            User user = new User(null,shopOwner.getNo(), shopOwner .getNo(), actor, null);
            Integer id = UserService.getInstance().add(user,conn);
            shopOwner.setId(id);
            added = shopOwnerDao.add(shopOwner,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return added;
    }

    public boolean update(ShopOwner shopOwner)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean update = false;
        try {
            update = shopOwnerDao.update(shopOwner,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return update;
    }

    public boolean delete(Integer id)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean deleted = false;
        try {
            deleted = shopOwnerDao.delete(id,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return deleted;
    }
}

